const model = require("../model/goods");

async function addGoods(goods) {
    const result = await model.addGoods(goods);
    if (!result.res.insertedId) {
        return { msg: "添加商品失败", result };
    } else {
        return { msg: "添加商品成功", result };
    }
}

async function deleteGoods(id) {
    const result = await model.deleteGoods(id);
    if (!result.res.deletedCount) {
        return { msg: "删除商品失败", result };
    } else {
        return { msg: "删除商品成功", result };
    }
}

async function updateGoods(id, goods) {
    const result = await model.updateGoods(id, goods);
    if (result.res.modifiedCount) {
        return { msg: "更新成功", result };
    } else {
        return { msg: "更新失败", result };
    }
}

const pageSize = 5;
function getGoodsByPage(page) {
    // 1: s0 l5
    // 2: s5 l5
    // 3: s10 l5
    return model.getGoods({}, { skip: pageSize * (page - 1), limit: pageSize });
}

/* limit为-1时代表取全部 */
function getAllGoods() {
    return model.getGoods({}, { skip: 0, limit: -1 });
}

module.exports = {
    addGoods,
    deleteGoods,
    updateGoods,
    getGoodsByPage,
    getAllGoods,
};
